package com.wootion.cims.module.communityservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.wootion.cims.dao.*;
import com.wootion.cims.general.utils.GetIp;
import com.wootion.cims.module.communitypersonal.model.Person;
import com.wootion.cims.module.communityresidence.residencelist.model.Residence;
import com.wootion.cims.module.communityservice.dto.MemberMsgDto;
import com.wootion.cims.module.communityservice.dto.PersonDto;
import com.wootion.cims.module.communityservice.excel.MemberExcel;
import com.wootion.cims.module.communityservice.model.Condition;
import com.wootion.cims.module.communityservice.model.PageQuery;
import com.wootion.cims.module.communityservice.service.MemberService;
import com.wootion.cims.module.systemsetting.community.model.SysCommunity;
import com.wootion.cims.module.systemsetting.log.model.SysLog;
import com.wootion.cims.module.systemsetting.polt.model.SysPlot;
import com.wootion.cims.module.systemsetting.user.model.SysUserRole;
import com.wootion.cims.module.systemsetting.user.model.User;
import com.wootion.cims.response.ResponseResult;
import com.wootion.cims.response.ResultCode;
import com.wootion.cims.shiro.utils.ShiroUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @Author: LanQiang
 * @Date: 2022/12/26/17:00
 * @Description: 作用xxx
 */
@Slf4j
@Service
public class MemberServiceImpl implements MemberService {
    @Resource
    private SysUserRoleDao sysUserRoleDao;
    @Resource
    private SysRoleDao sysRoleDao;
    @Resource
    private SysUserCommunityDao sysUserCommunityDao;
    @Resource
    private SysCommunityDao sysCommunityDao;
    @Resource
    private SysPlotDao sysPlotDao;
    @Resource
    private PersonDao personDao;
    @Resource
    private ResidenceDao residenceDao;
    @Value("${cims.path}")
    private String basePath;
    @Resource
    private SysLogDao sysLogDao;
    /**
     * 党员信息列表查询
     * @param pageQuery
     * @return
     */
    @Override
    public ResponseResult list(PageQuery pageQuery) {
        Integer page = pageQuery.getPage();
        Integer limit = pageQuery.getLimit();
        if(Objects.isNull(page) || Objects.isNull(limit)){
            return ResponseResult.builder().code(ResultCode.SYSTEM_ERROR.getCode()).message("页码数和每页显得的天数不能为空").build();
        }
        Condition condition = pageQuery.getCondition();
        //处理楼房详细地址(平房/别墅不用处理) 1栋/2单元
        //特殊处理详细地址
        String newDetailedAddress = null;
        if(condition != null && condition.getDetailedAddress() != null){
            if(condition.getDetailedAddress().size()>1){
                List<String> detailedAddress = condition.getDetailedAddress();
                newDetailedAddress  =  detailedAddress.get(0) +"/"+ detailedAddress.get(1);
            }else{
                List<String> detailedAddress = condition.getDetailedAddress();
                newDetailedAddress = detailedAddress.get(0);
            }
            condition.setAddress(newDetailedAddress);
        }
        String keyWord = null;
        String residenceCategory = null;
        String communityId = null;
        String plotId = null;
        String detailedAddress = null;
        String houseNumber = null;
        String gender = null;
        String nation = null;
        Date staterDate = null;
        Date endDate = null;
        Long joinPartyTime = null;//入党时间
        Long shiftToTime = null;//转入时间
        assert condition != null;
        keyWord = condition.getKeyWord();//关键字
        residenceCategory = condition.getResidenceCategory();//房屋类别
        communityId = condition.getCommunityId();//社区id
        plotId = condition.getPlotId();//小区id
        detailedAddress = condition.getAddress();//详细地址
        houseNumber = condition.getHouseNumber();//门牌号
        gender = condition.getGender();//性别
        nation = condition.getNation();//民族
        staterDate = condition.getStaterDate();//起始日期
        endDate = condition.getEndDate();//结束日期
        if(condition.getJoinPartyTime() != null){
            joinPartyTime = condition.getJoinPartyTime().getTime();//入党时间
        }
        if(condition.getShiftToTime() != null){
            shiftToTime = condition.getShiftToTime().getTime();//转入时间
        }
        //获取当前登陆人的信息
        User user = (User) ShiroUtils.getSubject().getPrincipal();
        LambdaQueryWrapper<SysUserRole> lambdaQueryWrapper5 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper5.eq(SysUserRole::getUserId, user.getId());
        SysUserRole sysUserRole = sysUserRoleDao.selectOne(lambdaQueryWrapper5);
        String roleKey = sysRoleDao.findByRoles(sysUserRole.getRoleId());
        //如果是街道管理员
        PageDTO<PersonDto> personDtos ;
        if ("street:admin".equals(roleKey)) {
            PageDTO<PersonDto> pageDTO = new PageDTO<>(page, limit);
            personDtos = personDao.memberList(pageDTO, null,keyWord,residenceCategory,communityId,plotId,detailedAddress,houseNumber,gender,nation,staterDate,endDate,joinPartyTime,shiftToTime);
        }else{
            //如果不是街道管理员
            List<String> commnityIds = sysUserCommunityDao.commnityIds(user.getId());
            if (Objects.isNull(commnityIds) || commnityIds.size() <= 0) {
                return ResponseResult.builder().code(ResultCode.SYSTEM_ERROR.getCode()).message("当前用户还没有任何社区权限").build();
            }
            condition.setCommunityIds(commnityIds);
            PageDTO<PersonDto> pageDTO = new PageDTO<>(page, limit);
            personDtos = personDao.memberList(pageDTO, commnityIds,keyWord,residenceCategory,communityId,plotId,detailedAddress,houseNumber,gender,nation,staterDate,endDate,joinPartyTime,shiftToTime);
        }
        if(!Objects.isNull(personDtos)){
            List<PersonDto> records = personDtos.getRecords();
            for (PersonDto record : records) {
                String abodeId = record.getAbodeId();//居住地住宅id
                Boolean inside2 = record.getInside2();//居住地区域内外标识，true是辖区内居住地
                if(StringUtils.hasText(abodeId) && inside2){
                    //辖区内
                    StringBuilder stringBuilder = new StringBuilder();
                    //处理居住地址
                    String communityid = record.getCommunityId();//社区id
                    LambdaQueryWrapper<SysCommunity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                    lambdaQueryWrapper.eq(SysCommunity::getId,communityid);
                    lambdaQueryWrapper.eq(SysCommunity::getEnable,true);
                    SysCommunity sysCommunity = sysCommunityDao.selectOne(lambdaQueryWrapper);
                    stringBuilder.append(sysCommunity.getName());

                    String plotId1 = record.getPlotId();//小区id
                    LambdaQueryWrapper<SysPlot> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
                    lambdaQueryWrapper1.eq(SysPlot::getId,plotId1);
                    lambdaQueryWrapper1.eq(SysPlot::getEnabled,true);
                    SysPlot sysPlot1 = sysPlotDao.selectOne(lambdaQueryWrapper1);
                    stringBuilder.append(sysPlot1.getName());

                    //住宅地址
                    String address = record.getAddress();
                    if(StringUtils.hasText(address)){
                        if(address.contains("/")){
                            String[] split = address.split("/");
                            stringBuilder.append(split[0]);//楼房楼栋
                            stringBuilder.append(split[1]);//楼房单元
                        }else {
                            stringBuilder.append(address);//平房/别墅
                        }
                    }
                    //门牌号
                    if(StringUtils.hasText(record.getHouseNumber())){
                        stringBuilder.append(record.getHouseNumber());
                    }
                    record.setAbode_address(stringBuilder.toString());
                }else {
                    StringBuilder stringBuilder = new StringBuilder();
                    //辖区外
                    String out2Province = record.getOut2Province();//省
                    if(StringUtils.hasText(out2Province)){
                        stringBuilder.append(out2Province);
                    }
                    String out2City = record.getOut2City();//市
                    if(StringUtils.hasText(out2City)){
                        stringBuilder.append(out2City);
                    }
                    String out2FullAddress = record.getOut2FullAddress();//辖区外居住详细地址
                    if(StringUtils.hasText(out2FullAddress)){
                        stringBuilder.append(out2FullAddress);
                    }
                    record.setAbode_address(stringBuilder.toString());
                }

                String placeId = record.getPlaceId();//户籍地住宅id
                Boolean inside1 = record.getInside1();//户籍地区域内外标识，true是辖区内户籍地
                if(StringUtils.hasText(placeId) && inside1){
                    LambdaQueryWrapper<Residence> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                    lambdaQueryWrapper.eq(Residence::getId,placeId);
                    lambdaQueryWrapper.eq(Residence::getEnabled,true);
                    Residence residence = residenceDao.selectOne(lambdaQueryWrapper);

                    LambdaQueryWrapper<SysCommunity> wrapper = new LambdaQueryWrapper<>();
                    wrapper.eq(SysCommunity::getId,residence.getCommunityId());
                    wrapper.eq(SysCommunity::getEnable,true);
                    SysCommunity sysCommunity = sysCommunityDao.selectOne(wrapper);
                    StringBuilder stringBuilder = new StringBuilder();
                    stringBuilder.append(sysCommunity.getName());//社区名称
                    LambdaQueryWrapper<SysPlot> wrapper2 = new LambdaQueryWrapper<>();
                    wrapper2.eq(SysPlot::getId,residence.getPlotId());
                    wrapper2.eq(SysPlot::getEnabled,true);
                    SysPlot sysPlot = sysPlotDao.selectOne(wrapper2);
                    stringBuilder.append(sysPlot.getName());//小区名称
                    //住宅地址
                    String address = residence.getAddress();
                    if(StringUtils.hasText(address)){
                        if(address.contains("/")){
                            String[] split = address.split("/");
                            stringBuilder.append(split[0]);//楼房楼栋
                            stringBuilder.append(split[1]);//楼房单元
                        }else {
                            stringBuilder.append(address);//平房/别墅
                        }
                    }
                    //门牌号
                    if(StringUtils.hasText(residence.getHouseNumber())){
                        stringBuilder.append(residence.getHouseNumber());
                    }
                    record.setPlace_address(stringBuilder.toString());
                }else {
                    StringBuilder stringBuilder = new StringBuilder();
                    //辖区外户籍地址
                    String out1Province = record.getOut1Province();//省
                    if(StringUtils.hasText(out1Province)){
                        stringBuilder.append(out1Province);
                    }
                    String out1City = record.getOut1City();//市
                    if(StringUtils.hasText(out1City)){
                        stringBuilder.append(out1City);
                    }
                    String out1FullAddress = record.getOut1FullAddress();//辖区外户籍详细地址
                    if(StringUtils.hasText(out1FullAddress)){
                        stringBuilder.append(out1FullAddress);
                    }
                    record.setPlace_address(stringBuilder.toString());
                }
                //如果是false,则代表辖区外 -居住地址 省-市-详细地址
                //处理信息完整度
                String partMan = record.getPartMan();//获取党员人的json字符串
                JSONObject jsonObject = JSON.parseObject(partMan);
                String integrity = (String) jsonObject.get("integrity");
                record.setIntegrity(integrity);
            }
            return ResponseResult.builder().data(personDtos).build();
        }
        return ResponseResult.builder().code(ResultCode.SYSTEM_ERROR.getCode()).message(ResultCode.SYSTEM_ERROR.getMessage()).build();
    }
    /**
     * 党员信息修改保存
     * @param personDto
     * @return
     */
    @Override
    public ResponseResult editSave(PersonDto personDto, HttpServletRequest request) {
        LambdaQueryWrapper<Person> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Person::getId,personDto.getId());
        lambdaQueryWrapper.eq(Person::getEnabled,true);
        Person person = personDao.selectOne(lambdaQueryWrapper);
        String partMan = person.getPartMan();
        MemberMsgDto memberMsgDto = JSON.parseObject(partMan, MemberMsgDto.class);
        MemberMsgDto memberMsgDto1 = personDto.getMemberMsgDto();//拿到修改后党员信息
        SysLog sysLog = new SysLog();
        sysLog.setIncident("党员信息");
        sysLog.setType("修改");
        sysLog.setValue("业务日志");
        sysLog.setCreateTime(new Date());
        sysLog.setIp(GetIp.getIpAddr(request));
        User user2 = (User) ShiroUtils.getSubject().getPrincipal();
        sysLog.setOperator(user2.getUsername());
        sysLog.setContent(person.getName()+"("+person.getIdNumber()+")");
        StringBuilder builder = new StringBuilder();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        if(memberMsgDto.getJoinPartyTime() != null){
            if(memberMsgDto1.getJoinPartyTime() != null){
                if(!memberMsgDto.getJoinPartyTime().equals(memberMsgDto1.getJoinPartyTime())){
                    builder.append("入党时间,修改前:").append(simpleDateFormat.format(memberMsgDto.getJoinPartyTime())).append(",修改后:").append(simpleDateFormat.format(memberMsgDto1.getJoinPartyTime())).append("。");
                }
            }else {
                builder.append("入党时间,修改前:").append(simpleDateFormat.format(memberMsgDto.getJoinPartyTime())).append(",修改后:无").append("。");

            }
        }else{
            if(memberMsgDto1.getJoinPartyTime() != null){
                builder.append("入党时间,修改前:无").append(",修改后:").append(simpleDateFormat.format(memberMsgDto1.getJoinPartyTime())).append("。");
            }
        }
        if(!memberMsgDto.getPartyPosts().equals(memberMsgDto1.getPartyPosts())){
            builder.append("党内职务,修改前:").append(memberMsgDto.getPartyPosts()).append(",修改后:").append(memberMsgDto1.getPartyPosts()).append("。");
        }
        if(!memberMsgDto.getPartyManCategory().equals(memberMsgDto1.getPartyManCategory())){
            builder.append("党员类别,修改前:").append(memberMsgDto.getPartyManCategory()).append(",修改后:").append(memberMsgDto1.getPartyManCategory()).append("。");
        }
        if(memberMsgDto.getShiftToTime() != null){
            if(memberMsgDto1.getShiftToTime() != null){
                if(!memberMsgDto.getShiftToTime().equals(memberMsgDto1.getShiftToTime())){
                    builder.append("转入时间,修改前:").append(simpleDateFormat.format(memberMsgDto.getShiftToTime())).append(",修改后:").append(simpleDateFormat.format(memberMsgDto1.getShiftToTime())).append("。");
                }
            }else {
                builder.append("转入时间,修改前:").append(simpleDateFormat.format(memberMsgDto.getShiftToTime())).append(",修改后:无").append("。");
            }
        }else{
            if(memberMsgDto1.getShiftToTime() != null){
                builder.append("转入时间,修改前:无").append(",修改后:").append(simpleDateFormat.format(memberMsgDto1.getShiftToTime())).append("。");
            }
        }
        if(memberMsgDto.getShiftOutTime() != null){
            if(memberMsgDto1.getShiftOutTime() != null){
                if(!memberMsgDto.getShiftOutTime().equals(memberMsgDto1.getShiftOutTime())){
                    builder.append("转出时间,修改前:").append(simpleDateFormat.format(memberMsgDto.getShiftOutTime())).append(",修改后:").append(simpleDateFormat.format(memberMsgDto1.getShiftOutTime())).append("。");
                }
            }else {
                builder.append("转出时间,修改前:").append(simpleDateFormat.format(memberMsgDto.getShiftOutTime())).append(",修改后:无").append("。");
            }
        }else {
            if(memberMsgDto1.getShiftOutTime() != null){
                builder.append("转出时间,修改前:无").append(",修改后:").append(simpleDateFormat.format(memberMsgDto1.getShiftOutTime())).append("。");
            }
        }

        if(!memberMsgDto.getPartyExpenditureBase().equals(memberMsgDto1.getPartyExpenditureBase())){
            builder.append("党费基数,修改前:").append(memberMsgDto.getPartyExpenditureBase()).append(",修改后:").append(memberMsgDto1.getPartyExpenditureBase()).append("。");
        }
        if(!memberMsgDto.getOriginalWorkUnit().equals(memberMsgDto1.getOriginalWorkUnit())){
            builder.append("原工作单位,修改前:").append(memberMsgDto.getOriginalWorkUnit()).append(",修改后:").append(memberMsgDto1.getOriginalWorkUnit()).append("。");
        }
        if(!memberMsgDto.getRemark().equals(memberMsgDto1.getRemark())){
            builder.append("其他备注,修改前:").append(memberMsgDto.getRemark()).append(",修改后:").append(memberMsgDto1.getRemark()).append("。");
        }
        BeanUtils.copyProperties(memberMsgDto1,memberMsgDto);
        String jsonString = JSON.toJSONString(memberMsgDto);
        person.setPartMan(jsonString);
        int rows = personDao.updateById(person);
        if(rows>0){
            sysLog.setResult("成功");
        }else{
            sysLog.setResult("失败");
        }
        sysLog.setDetails(builder.toString());
        int insert = sysLogDao.insert(sysLog);
        if (insert > 0) {
            log.info("日志记录成功");
        } else {
            log.error("日志记录失败");
        }
        return rows > 0 ? ResponseResult.builder().build() : ResponseResult.builder().message(ResultCode.SYSTEM_ERROR.getMessage()).code(ResultCode.SYSTEM_ERROR.getCode()).build();
    }
    /**
     * 党员信息列表导出
     * @param condition
     * @return
     */
    @Override
    public ResponseResult memberExcel(Condition condition) {
        //创建一个目录
        File dir = new File(basePath);
        if(!dir.exists()){
            //目录如果不存在就创建一个
            dir.mkdirs();
        }
        //处理楼房详细地址(平房/别墅不用处理) 1栋/2单元
        //特殊处理详细地址
        String newDetailedAddress = null;
        if(condition != null && condition.getDetailedAddress() != null){
            if(condition.getDetailedAddress().size()>1){
                List<String> detailedAddress = condition.getDetailedAddress();
                newDetailedAddress  =  detailedAddress.get(0) +"/"+ detailedAddress.get(1);
            }else{
                List<String> detailedAddress = condition.getDetailedAddress();
                newDetailedAddress = detailedAddress.get(0);
            }
            condition.setAddress(newDetailedAddress);
        }
        String keyWord = null;
        String residenceCategory = null;
        String communityId = null;
        String plotId = null;
        String detailedAddress = null;
        String houseNumber = null;
        String gender = null;
        String nation = null;
        Date staterDate = null;
        Date endDate = null;
        Long joinPartyTime = null;
        Long shiftToTime = null;
        List<String> personIds = null;
        assert condition != null;
        personIds = condition.getIds();//勾选的人员id
        keyWord = condition.getKeyWord();//关键字
        residenceCategory = condition.getResidenceCategory();//房屋类别
        communityId = condition.getCommunityId();//社区id
        plotId = condition.getPlotId();//小区id
        detailedAddress = condition.getAddress();//详细地址
        houseNumber = condition.getHouseNumber();//门牌号
        gender = condition.getGender();//性别
        nation = condition.getNation();//民族
        staterDate = condition.getStaterDate();//起始日期
        endDate = condition.getEndDate();//结束日期
        if(condition.getJoinPartyTime() != null){
            joinPartyTime = condition.getJoinPartyTime().getTime();//入党时间
        }
        if(condition.getShiftToTime() != null){
            shiftToTime = condition.getShiftToTime().getTime();//转入时间
        }
        //获取当前登陆人的信息
        User user = (User) ShiroUtils.getSubject().getPrincipal();
        LambdaQueryWrapper<SysUserRole> lambdaQueryWrapper5 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper5.eq(SysUserRole::getUserId, user.getId());
        SysUserRole sysUserRole = sysUserRoleDao.selectOne(lambdaQueryWrapper5);
        String roleKey = sysRoleDao.findByRoles(sysUserRole.getRoleId());
        //如果是街道管理员
        List<MemberExcel> memberExcelList = null;
        if ("street:admin".equals(roleKey)) {
            memberExcelList = personDao.memberExcel(personIds,null,keyWord,residenceCategory,communityId,plotId,detailedAddress,houseNumber,gender,nation,staterDate,endDate,joinPartyTime,shiftToTime);
        }else{
            //如果不是街道管理员
            List<String> commnityIds = sysUserCommunityDao.commnityIds(user.getId());
            if (Objects.isNull(commnityIds) || commnityIds.size() <= 0) {
                return ResponseResult.builder().code(ResultCode.SYSTEM_ERROR.getCode()).message("当前用户还没有任何社区权限").build();
            }
            condition.setCommunityIds(commnityIds);
            memberExcelList = personDao.memberExcel(personIds,commnityIds,keyWord,residenceCategory,communityId,plotId,detailedAddress,houseNumber,gender,nation,staterDate,endDate,joinPartyTime,shiftToTime);
        }
        if(!Objects.isNull(memberExcelList)){
            for (MemberExcel memberExcel : memberExcelList) {
                //处理居住地址
                String abodeId = memberExcel.getAbodeId();//居住地住宅id
                Boolean inside2 = memberExcel.getInside2();//居住地区域内外标识，true是辖区内居住地
                if(StringUtils.hasText(abodeId) && inside2){
                    LambdaQueryWrapper<Residence> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                    lambdaQueryWrapper.eq(Residence::getId,abodeId);
                    lambdaQueryWrapper.eq(Residence::getEnabled,true);
                    Residence residence = residenceDao.selectOne(lambdaQueryWrapper);

                    LambdaQueryWrapper<SysCommunity> wrapper = new LambdaQueryWrapper<>();
                    wrapper.eq(SysCommunity::getId,residence.getCommunityId());
                    wrapper.eq(SysCommunity::getEnable,true);
                    SysCommunity sysCommunity = sysCommunityDao.selectOne(wrapper);
                    StringBuilder stringBuilder = new StringBuilder();
                    stringBuilder.append(sysCommunity.getName());//社区名称
                    LambdaQueryWrapper<SysPlot> wrapper2 = new LambdaQueryWrapper<>();
                    wrapper2.eq(SysPlot::getId,residence.getPlotId());
                    wrapper2.eq(SysPlot::getEnabled,true);
                    SysPlot sysPlot = sysPlotDao.selectOne(wrapper2);
                    stringBuilder.append(sysPlot.getName());//小区名称
                    //住宅地址
                    String address = residence.getAddress();
                    if(StringUtils.hasText(address)){
                        if(address.contains("/")){
                            String[] split = address.split("/");
                            stringBuilder.append(split[0]);//楼房楼栋
                            stringBuilder.append(split[1]);//楼房单元
                        }else {
                            stringBuilder.append(address);//平房/别墅
                        }
                    }
                    //门牌号
                    if(StringUtils.hasText(residence.getHouseNumber())){
                        stringBuilder.append(residence.getHouseNumber());
                    }
                    memberExcel.setAbodeAddress(stringBuilder.toString());
                }else {
                    StringBuilder stringBuilder = new StringBuilder();
                    //辖区外
                    String out2Province = memberExcel.getOut2Province();//省
                    if(StringUtils.hasText(out2Province)){
                        stringBuilder.append(out2Province);
                    }
                    String out2City = memberExcel.getOut2City();//市
                    if(StringUtils.hasText(out2City)){
                        stringBuilder.append(out2City);
                    }
                    String out2FullAddress = memberExcel.getOut2FullAddress();//辖区外居住详细地址
                    if(StringUtils.hasText(out2FullAddress)){
                        stringBuilder.append(out2FullAddress);
                    }
                    memberExcel.setAbodeAddress(stringBuilder.toString());
                }
                //处理信息完整度
                String partMan = memberExcel.getPartMan();//获取党员的json字符串
                JSONObject jsonObject = JSON.parseObject(partMan);
                String integrity = (String) jsonObject.get("integrity");
                memberExcel.setIntegrity(integrity);
            }
            //开始写excel
            try {
                Date date = new Date();
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
                String format = simpleDateFormat.format(date);
                String filePathName = basePath + format + "党员信息.xlsx";
                EasyExcel.write(filePathName, MemberExcel.class).sheet("党员信息").doWrite(memberExcelList);
                return ResponseResult.builder().data(filePathName).build();
            } catch (Exception ex) {
                log.info("写党员信息列表excel时出现异常！");
                log.info(ex.getMessage());
                return ResponseResult.builder().code(ResultCode.SYSTEM_ERROR.getCode()).message("系统读写文件时，出现异常").build();
            }
        }
        return ResponseResult.builder().code(ResultCode.SYSTEM_ERROR.getCode()).message(ResultCode.SYSTEM_ERROR.getMessage()).build();
    }
    /**
     * 根基id查询党员信息
     * @param id
     * @return
     */
    @Override
    public ResponseResult find(String id) {
        LambdaQueryWrapper<Person> lambdaQueryWrapper3 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper3.eq(Person::getId,id);
        lambdaQueryWrapper3.eq(Person::getEnabled,true);
        Person person = personDao.selectOne(lambdaQueryWrapper3);

        PersonDto personDto = new PersonDto();
        BeanUtils.copyProperties(person,personDto);

        String abodeId = person.getAbodeId();//拿到住宅id
        Boolean inside2 = person.getInside2();//true是辖区内
        if(StringUtils.hasText(abodeId) && inside2){
            LambdaQueryWrapper<Residence> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(Residence::getId,abodeId);
            lambdaQueryWrapper.eq(Residence::getEnabled,true);
            Residence residence = residenceDao.selectOne(lambdaQueryWrapper);

            LambdaQueryWrapper<SysCommunity> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SysCommunity::getId,residence.getCommunityId());
            wrapper.eq(SysCommunity::getEnable,true);
            SysCommunity sysCommunity = sysCommunityDao.selectOne(wrapper);
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(sysCommunity.getName());//社区名称
            LambdaQueryWrapper<SysPlot> wrapper2 = new LambdaQueryWrapper<>();
            wrapper2.eq(SysPlot::getId,residence.getPlotId());
            wrapper2.eq(SysPlot::getEnabled,true);
            SysPlot sysPlot = sysPlotDao.selectOne(wrapper2);
            stringBuilder.append(sysPlot.getName());//小区名称
            //住宅地址
            String address = residence.getAddress();
            if(StringUtils.hasText(address)){
                if(address.contains("/")){
                    String[] split = address.split("/");
                    stringBuilder.append(split[0]);//楼房楼栋
                    stringBuilder.append(split[1]);//楼房单元
                }else {
                    stringBuilder.append(address);//平房/别墅
                }
            }
            //门牌号
            if(StringUtils.hasText(residence.getHouseNumber())){
                stringBuilder.append(residence.getHouseNumber());
            }
            personDto.setAbode_address(stringBuilder.toString());
        }else {
            StringBuilder stringBuilder = new StringBuilder();
            //辖区外
            String out2Province = person.getOut2Province();//省
            if(StringUtils.hasText(out2Province)){
                stringBuilder.append(out2Province);
            }
            String out2City = person.getOut2City();//市
            if(StringUtils.hasText(out2City)){
                stringBuilder.append(out2City);
            }
            String out2FullAddress = person.getOut2FullAddress();//辖区外居住详细地址
            if(StringUtils.hasText(out2FullAddress)){
                stringBuilder.append(out2FullAddress);
            }
            personDto.setAbode_address(stringBuilder.toString());
        }

        String placeId = person.getPlaceId();//户籍地住宅id
        Boolean inside1 = person.getInside1();//户籍地区域内外标识，true是辖区内户籍地
        if(StringUtils.hasText(placeId) && inside1){
            LambdaQueryWrapper<Residence> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(Residence::getId,placeId);
            lambdaQueryWrapper.eq(Residence::getEnabled,true);
            Residence residence = residenceDao.selectOne(lambdaQueryWrapper);

            LambdaQueryWrapper<SysCommunity> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SysCommunity::getId,residence.getCommunityId());
            wrapper.eq(SysCommunity::getEnable,true);
            SysCommunity sysCommunity = sysCommunityDao.selectOne(wrapper);
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(sysCommunity.getName());//社区名称
            LambdaQueryWrapper<SysPlot> wrapper2 = new LambdaQueryWrapper<>();
            wrapper2.eq(SysPlot::getId,residence.getPlotId());
            wrapper2.eq(SysPlot::getEnabled,true);
            SysPlot sysPlot = sysPlotDao.selectOne(wrapper2);
            stringBuilder.append(sysPlot.getName());//小区名称
            //住宅地址
            String address = residence.getAddress();
            if(StringUtils.hasText(address)){
                if(address.contains("/")){
                    String[] split = address.split("/");
                    stringBuilder.append(split[0]);//楼房楼栋
                    stringBuilder.append(split[1]);//楼房单元
                }else {
                    stringBuilder.append(address);//平房/别墅
                }
            }
            //门牌号
            if(StringUtils.hasText(residence.getHouseNumber())){
                stringBuilder.append(residence.getHouseNumber());
            }
            personDto.setPlace_address(stringBuilder.toString());
        }else {
            StringBuilder stringBuilder = new StringBuilder();
            //辖区外户籍地址
            String out1Province = person.getOut1Province();//省
            if(StringUtils.hasText(out1Province)){
                stringBuilder.append(out1Province);
            }
            String out1City = person.getOut1City();//市
            if(StringUtils.hasText(out1City)){
                stringBuilder.append(out1City);
            }
            String out1FullAddress = person.getOut1FullAddress();//辖区外户籍详细地址
            if(StringUtils.hasText(out1FullAddress)){
                stringBuilder.append(out1FullAddress);
            }
            personDto.setPlace_address(stringBuilder.toString());
        }
        //处理信息完整度
        String partMan = person.getPartMan();
        MemberMsgDto memberMsgDto = JSON.parseObject(partMan, MemberMsgDto.class);
        String integrity = memberMsgDto.getIntegrity();
        personDto.setIntegrity(integrity);
        personDto.setMemberMsgDto(memberMsgDto);
        return ResponseResult.builder().data(personDto).build();
    }
}
